var prcmd = require("ProtoCmdID")
var g_gameDataManager = require("g_gameDataManager");
var MessageManager = require("MessageManager");
cc.Class({
    extends: cc.Component,

    properties: {
        roomLabel:cc.Label,
        rolesViewContent:cc.Node,
        roomRole:cc.Prefab,
        readyBtn:cc.Button,
        rolesLis:[require('RoomRole')]
    },

    // use this for initialization
    onLoad: function () {
        var eventObj = {};
        eventObj.msg = g_gameDataManager.msgTemp;
        this.waitRoom(eventObj);
        NotificationCenter.on(prcmd.Wait_Room,this.waitRoom,this);
        NotificationCenter.on(prcmd.READY_ROOM,this.readyRoom,this);
        NotificationCenter.on(prcmd.KICK_PLAYER,this.kicked,this);
        NotificationCenter.on(prcmd.GAME_START,this.gameStart,this);
    },

    addRole:function(headUrl,isReady,usrID)
    {
        var role = cc.instantiate(this.roomRole);
        var rolesCount = this.rolesLis.length;
        var pos = this.getRolePos(rolesCount);
        role.setPosition(pos);
        this.rolesViewContent.addChild(role);
        var roomRoleCon = role.getComponent(require('RoomRole'));
        roomRoleCon.loadHeadUrl(headUrl);
        roomRoleCon.changeState(isReady);
        roomRoleCon.setUsrID(usrID);
        this.rolesLis.push(roomRoleCon);
    },

    getRolePos:function(index)
    {
        var offsetPos = cc.p(-235,-130);
        var disX = 235;
        var disY = -235;
        var addPos = cc.p(index%3*disX,parseInt(index/3)*disY);
        var pos = cc.pAdd(offsetPos,addPos);
        return pos;
    },

    removeRole:function(usrID)
    {
        for(var i=0;i<this.rolesLis.length;i++)
        {
            var roomRole = this.rolesLis[i];
            if(roomRole.usrID == usrID)
            {
                var prePos = roomRole.node.getPosition();
                for(var j=i+1;j<this.rolesLis.length;j++)
                {
                    var curPos = this.rolesLis[j].node.getPosition();
                    this.rolesLis[j].node.setPosition(prePos);
                    prePos = curPos;
                }
                this.rolesLis.splice(i, 1);
                roomRole.node.removeFromParent();
                break;
            }
        }
    },

    updateRoleState:function(usrID,isReady)
    {
        for(var i=0;i<this.rolesLis.length;i++) {
            var roomRole = this.rolesLis[i];
            if (roomRole.usrID == usrID) {
                roomRole.changeState(isReady);
            }
        }
    },

    readyCallBack:function(event)
    {
        //this.readyBtn.enabled = false;
        console.log("exitCallBack");
        var msg = {
            'cmdID': prcmd.READY_ROOM,
            'data': {
                'isReady': true
            }
        };
        MessageManager.sendMessage(JSON.stringify(msg));
    },

    exitCallBack:function(event)
    {
        //this.readyBtn.enabled = false;
        console.log("exitCallBack");
        var msg = {
            'cmdID': prcmd.EXIT_ROOM,
            'data': {
                'usrID':g_gameDataManager.getUserID()
            }
        };
        MessageManager.sendMessage(JSON.stringify(msg));
    },

    waitRoom:function(eventObj)
    {
        var roleLis = eventObj.msg.data.rolesInfo;
        for(var i=0;i<roleLis.length;i++)
        {
            var obj = roleLis[i];
            var hasAdd = false;
            for(var j=0;j<this.rolesLis.length;j++)
            {
                if(this.rolesLis[j].usrID == obj.usrID)
                {
                    hasAdd = true;
                    break;
                }
            }
            if(hasAdd == false)
            {
                this.addRole(obj.headUrl,obj.isReady,obj.usrID);
            }
        }
    },

    readyRoom:function(eventObj)
    {
        var roleLis = eventObj.msg.data.rolesInfo;
        for(var i=0;i<roleLis.length;i++)
        {
            var obj = roleLis[i];
            for(var j=0;j<this.rolesLis.length;j++) {
                if (this.rolesLis[j].usrID == obj.usrID) {
                    this.rolesLis[i].changeState(obj.isReady);
                }
            }
        }
    },

    kicked:function(eventObj)
    {
        console.log("kicked");
        var data = eventObj.msg.data;
        if(data.kickedUsrID == g_gameDataManager.getUserID())
        {
            var reason = data.reason;
            if(reason == prcmd.KICKED_BY_OVERTIME)
            {
                console.log("you were kicked for long time not start game");
            }
            else if(reason == prcmd.KICKED_BY_OTHER)
            {
                var usrName = data.usrName;
                console.log("you were kicked by"+usrName);
            }
            else if(reason == prcmd.KICKED_BY_SELF)
            {
                console.log("self into msg");
            }
            g_gameDataManager.msgTemp = data.hallInfo;
            cc.director.loadScene('HomeScene');
        }
        else
        {
            var kickMsg = '玩家['+data.kickedUsrName+']'+'被['+data.byUsrName+'] 踢出房间';
            console.log(kickMsg);
            var roleLis = data.rolesInfo;
            for(var i=0;i<roleLis.length;i++)
            {
                var obj = roleLis[i];
                for(var j=0;j<this.rolesLis.length;j++)
                {
                    if(this.rolesLis[j].usrID == obj.usrID)
                    {
                        this.rolesLis[j].node.removeFromParent();
                        this.rolesLis.splice(j,1);
                        break;
                    }
                }
            }

            for(var j=0;j<this.rolesLis.length;j++)
            {
                this.rolesLis[j].node.setPosition(this.getRolePos(j));
            }
        }
    },


    gameStart:function(eventObj)
    {
        console.log("gameStart");
        var msg = eventObj.msg;
        g_gameDataManager.msgTemp = msg;
        cc.director.loadScene('Minesweeper_Net');
    },

    onDestroy:function()
    {
        NotificationCenter.off(prcmd.Wait_Room,this.waitRoom,this);
        NotificationCenter.off(prcmd.READY_ROOM,this.readyRoom,this);
        NotificationCenter.off(prcmd.KICK_PLAYER,this.kicked,this);
    },



    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
